package com.lili.member.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lili.common.result.Result;
import com.lili.member.bean.Member;
import com.lili.member.service.MemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/hy")
@Api(tags = "会员管理")
public class MemberController {
    @Autowired
    MemberService memberService;

    @ApiOperation(value = "新增会员")
    @PostMapping("/save")
    public Result saveMember(@RequestBody Member member) {
        int rs = memberService.save(member);
        return rs == 1 ? Result.ok() : Result.fail();
    }

    @ApiOperation(value = "查询会员")
    @PostMapping("/query")
    public Result queryMember(@RequestBody Map map) {
        int current = 1;
        int pageSize = 10;
        if (map.get("current") != null) {
            current = Integer.parseInt(map.get("current").toString());
        }
        if (map.get("pageSize") != null) {
            pageSize = Integer.parseInt(map.get("pageSize").toString());
        }
        Page<Member> page = memberService.queryByPage(current, pageSize,map);
        return Result.ok(page);
    }

    @ApiOperation(value = "更新会员")
    @PutMapping("/update")
    public Result updateMember(@RequestBody Member member) {
        int rs = memberService.update(member);
        return rs == 1 ? Result.ok() : Result.fail();
    }

    @ApiOperation("禁用会员")
    @DeleteMapping("/update/delete")
    public Result deleteMember(@RequestParam String id) {
        int rs = memberService.updateState(id);
        return rs == 1 ? Result.ok() : Result.fail();
    }

    @ApiOperation("启用会员")
    @DeleteMapping("/update/restart")
    public Result restartMember(@RequestParam String id) {
        int rs = memberService.updateStateRestart(id);
        return rs == 1 ? Result.ok() : Result.fail();
    }


}